/*
 * Copyright 2012 ITC Tsinghua University. All Rights Reserved.
 */
package cn.edu.tsinghua.cic.ccss.qo;

import java.util.Date;

import org.forten.core.dao.rdb.hibernate.QueryInfo;
import org.forten.core.dao.rdb.query.Operator;
import org.forten.core.dao.rdb.query.parameter.Type;
import org.forten.core.qo.QueryEntity;
import org.forten.core.qo.QueryField;
import org.forten.core.qo.QueryObject;
import org.forten.utils.common.DateUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.tsinghua.cic.ccss.entity.Course;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * 
 * @since 2012-5-10
 */
@QueryEntity(Course.class)
@Component("courseQo")
@Scope("prototype")
public class CourseQo implements QueryObject {
	@QueryField(fieldName = "name", paramName = "name", operator = Operator.LIKE)
	private String name;
	@QueryField(fieldName = "teacherName", paramName = "teacherName", operator = Operator.LIKE)
	private String teacherName;
	@QueryField(fieldName = "teachTime", paramName = {"start","end"},operator = Operator.BETWEEN,type= Type.DATE)
	private Date[] teachTimePeriod;
	private String start;
	private String end;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.forten.core.qo.QueryObject#beforeBuildQueryInfo()
	 */
	@Override
	public void beforeBuildQueryInfo() {
		//若起始时间不为null，并且结束时间为null，则查询当前系统时间的所有的课程
		if(start !=null && "".equals(end) && !"".equals(start)){
			teachTimePeriod = new Date[2];
			teachTimePeriod[0] = DateUtil.convertStringToDate(start, "yyyyMMdd");
			Date currDate = DateUtil.currentTime();
			end = DateUtil.convertDateToString(currDate, "yyyyMMdd");
			teachTimePeriod[1] = DateUtil.convertStringToDate(end, "yyyyMMdd");
		} else 
		//若起始时间和结束时间均为null
		if(start != null && end != null && !"".equals(start) && !"".equals(end)){
			teachTimePeriod = new Date[2];
			teachTimePeriod[0] = DateUtil.convertStringToDate(start, "yyyyMMdd");
			teachTimePeriod[1] = DateUtil.convertStringToDate(end, "yyyyMMdd");
		}
		//不符合规则的全取
		 else {
			teachTimePeriod = null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.forten.core.qo.QueryObject#afterBuildQueryInfo(org.forten.core.dao
	 * .rdb.hibernate.QueryInfo)
	 */
	@Override
	public QueryInfo afterBuildQueryInfo(QueryInfo qi) {
		
		return qi;
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	
	public Date[] getTeachTimePeriod() {
		return teachTimePeriod;
	}

	public void setTeachTimePeriod(Date[] teachTimePeriod) {
		this.teachTimePeriod = teachTimePeriod;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.forten.core.qo.QueryObject#clean()
	 */
	
	@Override
	public void clean() {
		this.name = null;
		this.teacherName = null;
		this.start = null;
		this.end = null;
	}

	@Override
	public String toString() {
		return "CourseQo [name=" + name + ", teacherName="
				+ teacherName + "]";
	}
}
